//
//  HQCircleLayout.m
//  Dsgfds
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 胡泉. All rights reserved.
//

#import "HQCircleLayout.h"
#define ITEM_SIZE 72
@implementation HQCircleLayout
- (void)prepareLayout
{
    [super prepareLayout];
    CGSize size = self.collectionView.bounds.size;
    _cellCount = [self.collectionView numberOfItemsInSection:0];
    // 计算环的圆心
    _center = CGPointMake(size.width / 2.0, size.height / 2.0);
    // 计算环的半径（以宽高中较小的2.5分之一作为半径）
    _radius = MIN(size.width, size.height) / 2.5;
    
    
}


- (CGSize)collectionViewContentSize
{
    return self.collectionView.bounds.size;
}

// 返回指定单元格的大小和位置

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建一个返回对象
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
    
    // 设置单元格的中心点坐标 下列用三角函数计算坐标
    attributes.center = CGPointMake(_center.x + _radius * cosf(2 * M_PI * indexPath.item / _cellCount), _center.y + _radius * sinf(2 * M_PI * indexPath.item / _cellCount));
    return attributes;
    
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *attributes = [NSMutableArray array];
    // 将上个方法控制每个单元格的大小和位置的attributes添加到array中
    for (int i = 0; i < _cellCount; i++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        
    }
    return attributes;
    
}


// 每当单元格动态显示时自动调用
//- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//{
//    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
//    attributes.alpha = 0.0;
//    attributes.center = CGPointMake(_center.x, _center.y);
//    return attributes;
//}

// 每当单元格动态消失时自动调用
//- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//{
//    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
//    attributes.alpha = 0.0;
//    attributes.center = CGPointMake(_center.x, _center.y);
//    attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 0.1);
//    return attributes;
//}

@end
